// pages/learning/learning.js
var network = require("../../api/api.js")
const util = require('../../utils/util.js')

Page({
  data: {
    selected: true,
    selected1: false,
    img: "../../images/cz.png",
  },
  selected: function (e) {
    this.setData({
      selected1: false,
      selected: true
    })
  },
  selected1: function (e) {
    this.setData({
      selected: false,
      selected1: true
    })
  },
  // 确认已读
  readFn: function (e) {
    wx.setStorageSync('isRead', 1)
    var userLogin = wx.getStorageSync('user_login') || {};
    util.authJudge(userLogin)
  },
  goDetail: function (e) {
    const num = e.currentTarget.dataset.key || 1
    wx.navigateTo({
      url: `../dictor/dictor?tab=${num}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var isRead = wx.getStorageSync('isRead') || 0
    let noBottom = isRead == 1 ? 'noBottom' : ''
    this.setData({ isRead, noBottom })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('hide')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.setStorageSync('isRead', 1)
    var userLogin = wx.getStorageSync('user_login') || {}
    if (userLogin.prompt_read_status === 1) {
      var params = new Object()
      params.business_param = {}
      params.business_param.user_id = String(userLogin.user_id)
      network.POST({
        params: params,
        url: "read_abstract",
        success: function (res) {
          if (res.result_state === '0') {
            userLogin = Object.assign({}, userLogin, {
              prompt_read_status: 0
            })
            wx.setStorageSync('user_login', userLogin)
            wx.reLaunch({
              url: '../index/index'
            })
          }
        },
        fail: function () {
          //失败后的逻辑

        },
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})